using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animator))]

// PLACE WITH ANIMATOR

public class AnimateTroll : MonoBehaviour {
	
	private Animator anim;							// a reference to the animator on the character
	private AnimatorStateInfo currentBaseState;			// a reference to the current state of the animator, used for base layer
	private AnimatorStateInfo torsoCurrentState;
	private AnimatorStateInfo aimingCurrentState;
	
	public Transform fpsCamera;
	
	static int idleState = Animator.StringToHash("Base Layer.Idle");	
	static int runState = Animator.StringToHash("Base Layer.Run");
	static int jumpState = Animator.StringToHash("Base Layer.Jump");
	static int jumpEndState = Animator.StringToHash("Base Layer.JumpEnd");
	static int knifeMeleeState = Animator.StringToHash("Knife.KnifeMelee");
	static int knifeRangedState = Animator.StringToHash("Knife.KnifeShoot");
	static int revolverMeleeState = Animator.StringToHash("Revolver.RevolverMelee");
	static int revolverRangedState = Animator.StringToHash("Revolver.RevolverShoot");
	static int spudGunMeleeState = Animator.StringToHash("SpudGun.SpudGunMelee");
	static int spudGunRangedState = Animator.StringToHash("SpudGun.SpudGunShoot");
	static int miscMeleeState = Animator.StringToHash("Miscellaneous.MiscMelee");
	static int miscRangedState = Animator.StringToHash("Miscellaneous.MiscShoot");
	
	private Vector3 oldPos;
	private Quaternion oldRot;
	private float updateAnimation = 0.0f;
	
	private float currentRot;
	private float maxAngle = 60f;
	
	public float moveCameraForward = 0.5f, moveCameraUp = 1.5f;
	public Transform moveCameraToPoint;
	
	private bool playLand = true;

	
	// Use this for initialization
	void Start() {
		
		anim = GetComponent<Animator>();
		
		if (anim.layerCount == 3) {
			anim.SetLayerWeight(1, 1);
			anim.SetLayerWeight(2, 1);
		}
		
		
		anim.SetBool("Unarmed", true);
		
		oldPos = transform.position;
		oldRot = transform.rotation;
	}
	
	// Update is called once per frame
	void Update() {			
		
		currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
		if (anim.layerCount >= 2){
			torsoCurrentState = anim.GetCurrentAnimatorStateInfo(1);
			
		}
		
		//TURNING THE CHARACTER
		Vector3 forward = fpsCamera.forward,
				right 	= fpsCamera.right;

		forward.y 	= 0f;
		right.y 	= 0f;
		
		transform.localRotation = Quaternion.LookRotation(forward, Vector3.up);
		fpsCamera.position = moveCameraToPoint.position + forward * moveCameraForward + Vector3.up * moveCameraUp;
		
		//Aiming up/down
		currentRot = fpsCamera.transform.rotation.eulerAngles.x;
		
		if (currentRot >= 0 && currentRot <= 60){
			anim.SetFloat("AimAngle", (-1*currentRot/maxAngle));
		}
		
		else if (currentRot < 360 && currentRot >= 300){
			anim.SetFloat("AimAngle", 1-((currentRot-300)/maxAngle));
		}
		
		// Calculate delta
		Vector3 deltaPos 	= transform.position - oldPos;
		Quaternion deltaRot = transform.rotation * Quaternion.Inverse(oldRot);
		deltaPos.y = 0f;
		
		
		if (deltaPos.magnitude > 0.01 
		&&  !anim.GetBool("Jumping")) {

			// Move.
			deltaPos.Normalize();
			
			float scalarForward = Vector3.Dot(forward, deltaPos);
			float scalarRight	= Vector3.Dot(right, deltaPos);
			
			bool isRight   = (scalarRight > 0.0);
			bool isForward = (scalarForward > 0.5);
			bool isStrafe  = (scalarForward >= -0.5 && scalarForward <= 0.5);
			
			anim.SetBool("Run", 		isForward);
			anim.SetBool("BackPedal", 	!isForward && !isStrafe);
			anim.SetBool("StrafeRight", isStrafe && isRight);
			anim.SetBool("StrafeLeft", 	isStrafe && !isRight);
			
			updateAnimation = Time.time + 0.1f;

		} else if (Time.time > updateAnimation) {
			// Stop.
			anim.SetBool("Run", 		false);
			anim.SetBool("BackPedal", 	false);
			anim.SetBool("StrafeRight", false);
			anim.SetBool("StrafeLeft", 	false);
		}
		
		oldPos = transform.position;
		oldRot = transform.rotation;
		
		//AttackState Reset
		
		if (torsoCurrentState.nameHash == knifeMeleeState 
		||  torsoCurrentState.nameHash == revolverMeleeState 
		||  torsoCurrentState.nameHash == spudGunMeleeState
		||  torsoCurrentState.nameHash == miscMeleeState){
			anim.SetBool("MeleeAttack", false);
		}
		
		if (torsoCurrentState.nameHash == knifeRangedState
		||	torsoCurrentState.nameHash == revolverRangedState 
		||  torsoCurrentState.nameHash == spudGunRangedState
		||  torsoCurrentState.nameHash == miscRangedState){
			anim.SetBool("RangedAttack", false);
		}
		
		if(currentBaseState.nameHash == jumpEndState && playLand){
			playLand = false;
			if(transform.parent.gameObject.GetComponent<WeaponSystem>().enabled)
				Listeners.inst().notify(Channel.ON_LAND,GetComponent<PlayerState>());
		}
		
		if(currentBaseState.nameHash != jumpEndState){
			playLand = true;
		}
	}
}
